package oracle_jdbc_xml;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Oracle_JDBC_XML extends DefaultHandler {

    String dbName, userName, password, host;
    private String thisElement = "";

    @Override
    public void startDocument() throws SAXException {
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        thisElement = qName;
        // System.out.println("Start element="+qName);
        if (atts != null) {
            int len = atts.getLength();
            for (int i = 0; i < len; i++) {
                //System.out.println("Attribute="+ atts.getLocalName(i));
                // System.out.println("Attrbibute value="+atts.getValue(i));
            }
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        thisElement = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (thisElement == "user") {
            userName = value;
        } else if (thisElement == "password") {
            password = value;
        } else if (thisElement == "url") {
            host = value;
        } else if (thisElement == "dbname") {
            dbName = value;
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            Oracle_JDBC_XML jDBC_XML = new Oracle_JDBC_XML();
            parser.parse(new File("C:\\Users\\netcracker\\Documents\\NetBeansProjects\\Oracle_JDBC_XML\\XML\\DB.XML"), jDBC_XML);


            Locale locale = Locale.getDefault();
            System.out.println("Default Locale=" + locale);
            Locale.setDefault(Locale.ENGLISH);
            String resultString, reqString, url, user, password;

            Class.forName("oracle.jdbc.driver.OracleDriver");
            url = "jdbc:oracle:thin:@" + jDBC_XML.host + jDBC_XML.dbName;
            user = jDBC_XML.userName;
            password = jDBC_XML.password;
            Connection connection = DriverManager.getConnection(url, user, password);
            Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = statement.executeQuery("SELECT * FROM employees");


            while (rs.next()) {
                resultString = String.valueOf(rs.getInt(1) + " "
                        + rs.getString(2) + " " + rs.getString(2) + " "
                        + rs.getString(3) + " " + rs.getString(4) + " "
                        + rs.getDate("hire_date")) + " " + rs.getString("job_id") + " "
                        + rs.getInt("salary") + " " + rs.getDouble("COMMISSION_PCT") + " "
                        + rs.getInt("manager_id") + " " + rs.getInt("department_id");

                System.out.println(resultString);
            }
            System.out.println("=========================");



        } catch (ParserConfigurationException | SAXException ex) {
            System.out.println(ex.getMessage());
        }


    }
}
